package com.test.daily.leetcode.y2022.m06.day0606.v02;

import java.util.Stack;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/6/6 下午 08:29
 * @version: 1.0
 */
public class Solution {
    public static void hanio1(int n) {
        System.out.println("hanio1~");

    }

    public static void moveFromLeftToRight(int n) {
        if (n == 1) {
            System.out.println("move 1 from left to right~");
        }
        moveFromLeftToMid(n - 1);
        System.out.println("move " + n + " from left to right.");
        moveFromMidToRight(n - 1);
    }

    public static void moveFromLeftToMid(int n) {
        if (n == 1) {
            System.out.println("move 1 from left to mid");
        }
        moveFromLeftToRight(n - 1);
        System.out.println("move " + n + "from left to mid.");
        moveFromRightToMid(n - 1);
    }

    public static void moveFromMidToRight(int n) {
        if (n == 1) {
            System.out.println("move 1 from mid to right.");
        }
        moveFromMidToLeft(n - 1);
        System.out.println("move " + n + " from mid to right");
        moveFromLeftToRight(n - 1);
    }

    public static void moveFromRightToMid(int n) {
        if (n == 1) {
            System.out.println("move 1 from right to mid");
        }
        moveFromRightToLeft(n - 1);
        System.out.println("move " + n + " from right to mid.");
        moveFromLeftToMid(n - 1);
    }

    public static void moveFromMidToLeft(int n) {
        if (n == 1) {
            System.out.println("move 1 from mid to left");
        }
        moveFromMidToRight(n - 1);
        System.out.println("move " + n + " from mid to left");
        moveFromRightToMid(n - 1);
    }

    public static void moveFromRightToLeft(int n) {
        if (n == 1) {
            System.out.println("move 1 from right to left.");
        }
        moveFromRightToMid(n - 1);
        System.out.println("move " + n + " from right to left.");
        moveFromMidToLeft(n - 1);
    }

    public static void hanio2(int n) {

    }

    public static void func(int N, String from, String to, String other) {
        if (N == 1) {
            System.out.println("move 1 from " + from + " to " + to);
        }
        func(N - 1, from, other, to);
        System.out.println("move " + N + " from " + from + " to " + to);
        func(N - 1, other, to, from);
    }

    public static class Record {
        public boolean finish1;
        public int base;
        public String from;
        public String to;
        public String other;

        public Record(boolean f1, int b, String f, String t, String o) {
            finish1 = false;
            base = b;
            from = f;
            to = t;
            other = o;
        }
    }

    public static void hanio3(int N) {
        if (N < 1) {
            return;
        }
        Stack<Record> stack = new Stack<>();
        stack.add(new Record(false, N, "left", "right", "mid"));
        while (!stack.isEmpty()) {
            Record cur = stack.pop();
            if (cur.base == 1) {
                System.out.println("Move 1 from " + cur.from + " to " + cur.to);
                if (!stack.isEmpty()) {
                    stack.peek().finish1 = true;
                }
            } else {
                if (!cur.finish1) {
                    stack.push(cur);
                    stack.push(new Record(false, cur.base - 1, cur.from, cur.other, cur.to));
                } else {
                    System.out.println("Move " + cur.base + " from " + cur.from + " to " + cur.to);
                    stack.push(new Record(false, cur.base - 1, cur.other, cur.to, cur.from));
                }
            }
        }
    }

}
